package com.mi.rule.wx_pay.other;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * MybatisPlus
 * 基础配置
 *
 * @author 王永吉
 * @date 2020-01-14
 */
@Slf4j
@Configuration
public class MybatisPlusConfig {
    /**
     * 分页
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }

    /**
     * 驼峰转换器
     */
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setObjectWrapperFactory(new MiWrapperFactory());
    }

    /**
     * 驼峰转换器 MiWrapperFactory
     */
    static class MiWrapperFactory implements ObjectWrapperFactory {
        @Override
        public boolean hasWrapperFor(Object o) {
            return o instanceof Map;
        }

        @Override
        public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) {
            return new MiMapWrapper(metaObject, (Map) o);
        }

        static class MiMapWrapper extends MapWrapper {
            MiMapWrapper(MetaObject metaObject, Map<String, Object> map) {
                super(metaObject, map);
            }

            @Override
            public String findProperty(String name, boolean useCamelCaseMapping) {
                if (useCamelCaseMapping) return HumpKit._ToHump(name);
                return name;
            }
        }
    }

}



